function showSupplier() {
	mask.open(true);
	ajaxGet(website + "boSupplier/show", null, function(result) {
		var tabOrder = $("#currentTabOrder").val();
		$("#tabs-"+tabOrder).html(result);
		initSupplier();
		mask.close();
	}, "html");
}

function initSupplier() {
	$("#query-supplier").button().click(function() {
		search();
	});
	
	$("#create-supplier").button().click(function() {
		addOrEditSupDialog(ACTION_ADD);
	});
	
	$("#edit-supplier").button().click(function() {
		addOrEditSupDialog(ACTION_EDIT);
	});

	$("#delete-supplier").button().click(function() {
		deleteDialog(SUPPLIER,"checkKey");
	});

	$("#create-dish-category").button().click(function() {
		addOrEditDishCateDialog(ACTION_ADD,true);
	});

	$("#create-dish-bunddle").button().click(function() {
		addOrEditBunddle(ACTION_ADD);
	});
}

function addOrEditSupDialog(action, updateId) {
	var url = website + "boSupplier/insertPage";
	var params = null;
	if(action == ACTION_EDIT){
		var ids = null;
		if(updateId == null){
			ids = getSelectedItems("checkKey");
			if (ids == "" || ids.split(",").length != 1) {
				alert("请只选择一个商家");
				return;
			}
		}else{
			ids = updateId;
		}
		params = {
			supid : ids
		}
		url = website + "boSupplier/updatePage";
	}
	
	$("#curAction").val(action);
	
	mask.open(true);
	ajaxGet(url, params, function(result) { 
		$("#dialog-form-supplier").html(result);
		$("#dialog-form-supplier") .dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					saveSupplier();
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				$(this).html("");
			}
		});
		mask.close();
	}, "html");
}

function showDetail(obj){
	var params = {
		supid : obj
	}
	mask.open(true);
	ajaxGet(website + "boSupplier/detail", params, function(result) { 
		$("#dialog-form-supplier").html(result);
		$("#dialog-form-supplier") .dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"复制" : function() {
					$(this).dialog("close");
					copySupplier(obj);
				},
				"修改" : function() {
					$(this).dialog("close");
					addOrEditSupDialog(ACTION_EDIT,obj);
				},
				"确定" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				$(this).html("");
			}
		});
		mask.close();
	}, "html");
}

function copySupplier(supid){
	var params = {
		supid : supid
	}
	mask.open(true);
	ajaxPost(website + "boSupplier/copy", params, function(result) { 
		$("#dialog-message").html(result);
		$("#dialog-message").dialog({
			modal : true,
			resizable : false,
			buttons : {
				Ok : function() {
					showSupplier();
					$(this).dialog("close");
				}
			}
		});
		mask.close();
	}, "html");
}

function saveSupplier(){
	var name = $("#name");
	var address = $("#address");
	var phone = $("#phone");
	var phone2 = $("#phone2");
	var telephone = $("#telephone");
	var telephone2 = $("#telephone2");
	var startTime = $("#starttime");
	var endTime = $("#endtime");
	var leastPrice = $("#leastprice");
	var leastDistance = $("#leastdistance");
	var avgPrice = $("#avgprice");
	var sort = $("#sort");
	var file = $("#file");
	var deliveryDesc = $("#deliverydesc");
	var desc = $("#desc");
	var allFields = $([]).add(name).add(address).add(phone).add(phone2).add(telephone).add(telephone2).add(startTime)
	.add(endTime).add(leastPrice).add(leastDistance).add(avgPrice).add(sort)
	.add(file).add(deliveryDesc).add(desc), tips = $(".validateTips");
	
	var bValid = true;
	allFields.removeClass("ui-state-error");
	bValid = bValid&& checkLength(name, "名称",1, 16, tips);
	bValid = bValid&& checkLength(address, "地址",1, 100, tips);
	//bValid = bValid&& checkLength(phone, "电话",7, 16, tips);
	bValid = bValid&& checkLength(leastPrice, "起送金额",1, 16, tips);
	bValid = bValid&& checkLength(leastDistance, "外卖范围",1, 16, tips);
	bValid = bValid&& checkLength(avgPrice, "人均",1, 16, tips);
//	if($("#curAction").val() == ACTION_ADD){
//		bValid = bValid&& checkLength(file, "文件",1, 100, tips);
//	}
	bValid = bValid&& checkLength(deliveryDesc, "送餐说明",1, 200, tips);
	bValid = bValid&& checkLength(desc, "商家描述",1, 200, tips);
	
	if(phone.val()!=""){
		bValid = bValid && checkRegexp( phone, /^(\d{4}-\d{8})|(\d{12})$/, "固话1号码格式为0595-87654321或059587654321", tips);
	}
	if(phone2.val()!=""){
		bValid = bValid && checkRegexp( phone2, /^(\d{4}-\d{8})|(\d{12})$/, "固话2号码格式为0595-87654321或059587654321", tips);
	}
	if(telephone.val()!=""){
		bValid = bValid && checkRegexp( telephone, /^\d{3}-\d{4}-\d{4}$/, "手机号码格式为135-8765-4321", tips);
	}
	if(telephone2.val()!=""){
		bValid = bValid && checkRegexp( telephone2, /^\d{3}-\d{4}-\d{4}$/, "手机号码格式为135-8765-4321", tips);
	}
	bValid = bValid && checkRegexp( leastPrice, /^\d{1,9}$/, "起送金额必须为数字", tips);
	bValid = bValid && checkRegexp( leastDistance, /^\d{1,9}$/, "起送距离必须为数字", tips);
	bValid = bValid && checkRegexp( avgPrice, /^\d{1,9}$/, "人均必须为数字", tips);
	bValid = bValid && checkRegexp( sort, /^\d{1,9}$/, "排序必须为数字", tips);
	//bValid = bValid && compare(startTime,endTime, tips);
	
	if (bValid) {
		if($("#curAction").val() == ACTION_ADD){
			$("#supplierForm").submit();
		}
		else{
			var params = {
				id : $("#supplier_edit_id").val(),	
				sup_cate_id: $("#sup_cate_id").val(),
				name : name.val(),
				address : address.val(),
				road : $("#road").val(),
				phone : phone.val(),
				phone2 : phone2.val(),
				telephone : telephone.val(),
				telephone2 : telephone2.val(),
				starttime : startTime.val(),
				endtime : endTime.val(),
				leastprice : leastPrice.val(),
				leastdistance : leastDistance.val(),
				avgprice : avgPrice.val(),
				sort : sort.val(),
				deliverydesc : deliveryDesc.val(),
				desc : desc.val()
			}
			$.ajaxFileUpload
			(
				{
					url:website + "boSupplier/update",
					secureuri:false,
					fileElementId:'file',
					dataType: 'html',
					data:params,
					success: function (data, status)
					{
						if(typeof(data.error) != 'undefined')
						{
							if(data.error != '')
							{
								alert(data.error);
							}else
							{
								alert(data.msg);
							}
						}else{
							$("#dialog-form-supplier").dialog("close");
						}
						mask.close();
					},
					error: function (data, status, e)
					{
						alert(data.responseText);
						mask.close();
					}
				}
			);
		}
	}
}

function unApproveSupplier(obj){
	updateStatus(website + "boSupplier/unApprove", obj);
}

function approveSupplier(obj){
	updateStatus(website + "boSupplier/approve", obj);
}

function updateStatus(url, obj){
	var params = {
		supid : $(obj).attr("supplierid")
	}
	mask.open(true);
	ajaxGet(url,params,function(result){
		$("#status-" + $(obj).attr("supplierid")).html(result);
		$(obj).html("");
		$("#unApprove-" + $(obj).attr("supplierid")).html("");
		mask.close();
	},"html");
}

function openMapDialog(supplierid){
	var params = {
		supid : supplierid
	}
	$("#curSupplier").val(supplierid);
	mask.open(true);
	ajaxGet(website + "boSupplier/openMap",params,function(result){
		$("#map-dialog").html(result);
		$("#map-dialog").dialog({
			width : 1000,
			height: 500,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				getPage($("#currentPage").val());
			}
		});
		mask.close();
	},"html");
}

function search(){
	searchWithPage();
}

function getPage(pageIndex){
	searchWithPage(pageIndex);
}

function searchWithPage(pageIndex){
	var categoryType = $("#categoryType").val();
	var searchText = $("#searchText").val();
	var params = null;
	if(pageIndex != null){
		params = {
			categoryType:categoryType,
			searchText:searchText,
			page:pageIndex
		}
	}else{
		params = {
			categoryType:categoryType,
			searchText:searchText
		}
	}
	mask.open(true);
	ajaxGet(website + "boSupplier/search",params,function(result){
		var tabOrder = $("#currentTabOrder").val();
		$("#tabs-"+tabOrder).html(result);
		initSupplier();
		mask.close();
	},"html");
}